<!--药品信息管理-->
<template>
  <div style="padding: 10px">
    <div style="margin:10px">
      <el-button type="primary" @click="add" size="large">新增药品</el-button>
      <el-button >导入</el-button>
      <el-button >导出</el-button>
    </div>
    <div style="margin:10px 0">
      <el-input v-model="search" placeholder="请输入您要查找的药品的名称" style="width:30%"/>
      <el-button type="primary" style="margin-left:10px" @click="load">查询</el-button>
    </div>
    <el-table :data="tableData" stripe border style="width: 100%" clearable>
      <el-table-column prop="did" label="药品编号" sortable auto width="100"/>
      <el-table-column prop="dname" label="药品名称" width="180"/>
      <el-table-column prop="dclass" label="药品类别" width="100" />
      <el-table-column prop="dprice" label="药品价格" width="100" />
      <el-table-column prop="dspec" label="药品规格" width="100" />
      <el-table-column prop="dclaim" label="是否报销"  width="100" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗?" @confirm="handleDelete(scope.row.did)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>


    </el-table>
    <div style="margin: 10px">
      <el-pagination
          :page-size="pageSize"
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
      <el-dialog  title="新增" v-model="dialogVisible" width="30%">
        <el-form :model="form" label-width="120px">

          <el-form-item label="药品名称">
            <el-input v-model="form.dname" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="药品类别">
            <el-radio v-model="form.dclass" label="甲">甲</el-radio>
            <el-radio v-model="form.dclass" label="乙">乙</el-radio>
            <el-radio v-model="form.dclass" label="丙">丙</el-radio>
          </el-form-item>
          <el-form-item label="药品价格">
            <el-input v-model="form.dprice" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="药品规格">
            <el-input v-model="form.dspec" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item label="是否报销">
            <el-radio v-model="form.dclaim" label="1">是</el-radio>
            <el-radio v-model="form.dclaim" label="0">否</el-radio>
          </el-form-item>

        </el-form>
        <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">
            确定
          </el-button>
         </span>
        </template>
      </el-dialog
      >




    </div>

  </div>
</template>

<script>



import request from "../../utils/request"; //!!!!!!!

export default {
  name: 'Drug',
  components: {

  },
  data(){
    return{
      form:{},
      form1:{

      },
      form2:{

      },
      form4:{},
      dialogVisible:false,
      dialogVisible1:false,
      dialogVisible2:false,
      search:'',
      pageSize:10,
      currentPage:1,
      total:0,
      tableData: [],
      // rules: {
      //   学位课数量: [
      //     { required: true, message: '请输入学位课数量', trigger: 'blur' },
      //     { validator: this.validateNumberNotZero, trigger: 'blur' }
      //   ]
      // }

    }
  },
  created(){
    this.load()
  },
  methods:{
    load(){
      request.get("/drug",{
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    // choose(row) {
    //   if (row.sgrade === "研二") {
    //     this.form4.sgrade="研二";
    //     this.form4.sno=row.sno;
    //     this.dialogVisible1 = true;
    //   } else if (row.sgrade === "研三") {
    //     this.form.sgrade="研三";
    //     this.form4.sno=row.sno;
    //     this.dialogVisible2 = true;
    //   } else {
    //     this.$message({type: "error", message: "研一由学院统一规定,暂时不可申请!"})
    //   }
    // },
    add(){
      this.dialogVisible=true;
      this.form= {}
      this.load()

    },
    save(){
      if(this.form.did) {
        request.put("/drug", this.form).then(res => {
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type:"success",
              message:"更新成功"
            })
          } else{
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()
          this.dialogVisible = false //关闭弹窗
        })
      }else{ //新增
        request.post("/drug",this.form).then(res =>{
          console.log(res)
          if(res.code === '0'){
            this.$message({
              type:"success",
              message:"新增成功"
            })
          } else{
            this.$message({
              type:"error",
              message:res.msg
            })
          }
          this.load()
          this.dialogVisible=false
        })}

    },
    handleEdit(row){
      this.form =JSON.parse(JSON.stringify(row))
      this.dialogVisible =true
    },
    handleDelete(did){
      console.log(did)
      request.delete('/drug/'+ did).then(res =>{
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        } else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
      })
    },
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.currentPage = pageNum
      this.load()
    }

  }
}
</script>
